package knu;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * Created by Ray on 2014-12-10.
 */
@WebServlet(name = "SetGreet_Servlet")
public class SetGreet_Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String uri = request.getRequestURI();
        if(uri.equals("/SaveGreet"))
        {
            String greeting = request.getParameter("greeting");
            String greetingUTF8= URLEncoder.encode(greeting,"utf-8");
            response.addCookie(new Cookie("YourGreeting",greetingUTF8));
            response.getWriter().println("저장되었습니다. 다시방문해 주세요. <a href=\"/cookie.jsp\">다시방문하기</a>");
        }
        else if(uri.equals("/DeleteGreet")){
            Cookie cookie =new Cookie("YourGreeting",null);
            cookie.setMaxAge(0);
            cookie.setPath("/");
            response.addCookie(cookie);
            response.getWriter().println("삭제되었습니다. 다시방문해 주세요.<a href=\"/\">다시방문하기</a>");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
